﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using HQPad.Models;
using HQPad.Services;

namespace HQPad.DAL
{
    public class HQInitializer : CreateDatabaseIfNotExists<HQContext>
    {
        protected override void Seed(HQContext context)
        {
            // Create extra indexes.
            CreateIndex(context, "RuleSetID", typeof(Skill)); // add index on rules set id on skill table.

            // compound index on skill table for rulesetid and slug.
            context.Database.ExecuteSqlCommand("CREATE INDEX IX_Slug ON Skill (RuleSetId, Slug)");

            // rule set
            var rules = new List<RuleSet>
            {
                new RuleSet () {
                    Enabled = true, LastUpdated = DateTime.Now,
                    LastUpdatedByUserID = 1, MajorVersion = 1,
                    MinorVersion = 0}
            };
            rules.ForEach(s => context.RuleSets.Add(s));
            context.SaveChanges();

            int ruleSetId = rules[0].RuleSetID;

            // points tables.
            var tables = new List<PointsTable>
            {
                new PointsTable() { Race = "Human", Class = "Warrior", RuleSetID = ruleSetId },
                new PointsTable() { Race = "Human", Class = "Mage", RuleSetID = ruleSetId },
                new PointsTable() { Race = "Human", Class = "Acolyte", RuleSetID = ruleSetId },
                new PointsTable() { Race = "Human", Class = "Scout", RuleSetID = ruleSetId },
                new PointsTable() { Race = "Elf", Class = "Warrior", RuleSetID = ruleSetId },
                new PointsTable() { Race = "Elf", Class = "Mage", RuleSetID = ruleSetId },
                new PointsTable() { Race = "Elf", Class = "Acolyte", RuleSetID = ruleSetId },
                new PointsTable() { Race = "Elf", Class = "Scout", RuleSetID = ruleSetId },
                new PointsTable() { Race = "Half-orc", Class = "Warrior", RuleSetID = ruleSetId },
                new PointsTable() { Race = "Half-orc", Class = "Mage", RuleSetID = ruleSetId },
                new PointsTable() { Race = "Half-orc", Class = "Acolyte", RuleSetID = ruleSetId },
                new PointsTable() { Race = "Half-orc", Class = "Scout", RuleSetID = ruleSetId }
            };
            tables.ForEach(s => context.PointsTables.Add(s));
            context.SaveChanges();

            // import skills from spreadsheet.
            IImportService importService = new ImportService(context);

            string path = System.Web.HttpContext.Current.Server.MapPath("/Import/guilds.xlsx");

            ImportResult guildResult = importService.ImportGuilds(context, path, true, ruleSetId);

            // get spreadsheet path.
            path = System.Web.HttpContext.Current.Server.MapPath("/Import/skills.xlsx");

            ImportResult result = importService.ImportSkills(context, path, true, ruleSetId);

            /*
             *  Add spells.
             */
            path = System.Web.HttpContext.Current.Server.MapPath("/Import/spells.xlsx");

            ImportResult spellsResult = importService.ImportSpells(context, path, true, ruleSetId);

            /*
             *  Add invocations.
             */
            path = System.Web.HttpContext.Current.Server.MapPath("/Import/invocations.xlsx");

            ImportResult invocationResult = importService.ImportInvocations(context, path, true, ruleSetId);
        }

        private void CreateIndex(HQContext context, string field, Type table)
        {
            context.Database.ExecuteSqlCommand(String.Format("CREATE INDEX IX_{0} ON {1} ({0})", field, table.Name));
        } 
    }
}